package fragments.discover;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.wenxin.administrator.zhufengfm.Constants;
import com.wenxin.administrator.zhufengfm.R;

import java.util.ArrayList;
import java.util.List;

import adapter.DiscoverRecommendAdapter;
import entity.DiscoverRecommend.DiscoverRecommendItem;
import fragments.BaseFragment;
import tasks.DiscoverRecommendTask;
import tasks.TaskCallBack;
import tasks.TaskResult;

public class DiscoverRecommendFragment extends BaseFragment implements TaskCallBack {


    private DiscoverRecommendAdapter adapter;
    List<DiscoverRecommendItem> items;
    ListView listView;
    public DiscoverRecommendFragment() {
    }



    //必须在onCreate()创建,在创建视图之前，加载适配器
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        items = new ArrayList<>();
        adapter=new DiscoverRecommendAdapter(getActivity(), items);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_discover_recommend, container, false);
       listView = (ListView) ret.findViewById(R.id.discover_recommend_list);
        listView.setAdapter(adapter);
        //在显示UI之后更新适配器
        adapter.notifyDataSetChanged();
        return ret;
    }

    @Override
    public String getFragmentTitle() {
        return "推荐";
    }

    /**
     * 因为每次进入 推荐的时候 都会触发内容的刷新 所以在onresume写
     */
    public void onResume() {
        super.onResume();
        //todo   不懂
        DiscoverRecommendTask task=new DiscoverRecommendTask(this);
        task.execute();
    }

    @Override
    public void onTaskFinished(TaskResult result) {
        if(result!=null) {
            Log.d("RecommendFragment","result有错误"+result);
            if(result.action== Constants.TASK_ACTION_DISCOVER_RECOMMENDS) {
                if(result.resultCode==Constants.TASK_RESULT_OK){
                    Object data=result.data;
                    //todo 有错误

                    Log.d("RecommendFragment","data有错误"+data.toString());
                    if(data!=null&&data instanceof List){
                        List list= (List) data;
                        Log.d("RecommendFragment","list有错误"+list.toString());
                        //TODO: 15-10-21 更新UI
                        //只要数据来了，就要清除adapter
                        items.clear();
                        for (Object o:list) {
                            if(o instanceof DiscoverRecommendItem){
                                items.add((DiscoverRecommendItem) o);
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Log.d("RecommendFragment","Recommends错误");
                }
            }
        }
    }
}
